package jlxy.ise.eroad.util;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.ResponseHandlerInterface;

/**
 * Created by chen on 2017/4/28.
 */

public class WebUtil {

    //NetBeans地址
  //  private static final String BASE_URL = "http://172.25.247.1:9090";

    //Node.js地址
    private static final String BASE_URL = "http://172.21.74.1:3300";

    private static AsyncHttpClient client = new AsyncHttpClient();

    public static void get(String url, RequestParams params, ResponseHandlerInterface responseHandler) {
        client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void post(String url, RequestParams params, ResponseHandlerInterface responseHandler) {
        client.post(getAbsoluteUrl(url), params, responseHandler);
    }

    private static String getAbsoluteUrl(String relativeUrl) {
        return BASE_URL + relativeUrl;
    }


    //登录 http://127.0.0.1:9090/login/verify?telephone=15605182657&passwd=666666
    private static final String LOGIN_VERIFY_STR = "/login/verify";
    public static void postLoginVerify(RequestParams params, JsonHttpResponseHandler jsonHttpResponseHandler){
        WebUtil.post(LOGIN_VERIFY_STR, params, jsonHttpResponseHandler);
    }
    public static RequestParams getRpLoginVerify(String telephone, String passwd){
        RequestParams params = new RequestParams();
        params.add("telephone", telephone);
        params.add("passwd", passwd);
        return  params;
    }

    //忘记密码 http://127.0.0.1:9090/forget/verify?telephone=15605182657&drivingnumber=45042319870612921x
    private static final String FORGET_VERIFY = "/forget/verify";
    public static void postForgetVerify(RequestParams params, JsonHttpResponseHandler jsonHttpResponseHandler){
        WebUtil.post(FORGET_VERIFY, params, jsonHttpResponseHandler);
    }
    public static RequestParams getRpForgetVerify(String telephone, String drivingnumber){
        RequestParams params = new RequestParams();
        params.add("telephone", telephone);
        params.add("drivingnumber", drivingnumber);
        return  params;
    }

    //用户User注册(本课设不用) http://127.0.0.1:9090/app/addUser?nickname=nj&phone=13612345678&type=0&pwd=123456&email=0
    private static final String APP_ADDUSER = "/app/addUser";
    public static void postAddUser(RequestParams params, JsonHttpResponseHandler jsonHttpResponseHandler){
        WebUtil.post(APP_ADDUSER, params, jsonHttpResponseHandler);
    }
    public static RequestParams getRpAddUser(String telephone,String nickName,String pwd){
        RequestParams params = new RequestParams();
        params.add("phone", telephone);
        params.add("nickname", nickName);
        params.add("type", "0");
        params.add("PWD", pwd);
        params.add("email", "0");
        return  params;
    }

    //司机注册 http://127.0.0.1:9090/register/add1?telephone=13612345678&passwd=123456&registertime='2017-04-29 21:05:38'
    private static final String REGISTER_ADD1 = "/register/add1";
    public static void postAddDriverUser1(RequestParams params, JsonHttpResponseHandler jsonHttpResponseHandler){
        WebUtil.post(REGISTER_ADD1, params, jsonHttpResponseHandler);
    }
    public static RequestParams getRpAddDriverUser1(String telephone, String passwd, String registertime){
        RequestParams params = new RequestParams();
        params.add("telephone", telephone);
        params.add("passwd", passwd);
        params.add("registertime", registertime);
        return  params;
    }

    //司机注册2, http://127.0.0.1:9090/register/add2?id=1&name=王二麻子&drivingnumber=10086&emergencyperson=李四&emergencynumber=110
    // 填充个人信息
    private static final String REGISTER_ADD2 = "/register/add2";
    public static void postAddDriverUser2(RequestParams params, JsonHttpResponseHandler jsonHttpResponseHandler){
        WebUtil.post(REGISTER_ADD2, params, jsonHttpResponseHandler);
    }
    public static RequestParams getRpAddDriverUser2(String id,String name, String drivingnumber,
                                                    String emergencyperson, String emergencynumber){
        RequestParams params = new RequestParams();
        params.add("id", id);
        params.add("name", name);
        params.add("drivingnumber", drivingnumber);
        params.add("emergencyperson", emergencyperson);
        params.add("emergencynumber", emergencynumber);
        return  params;
    }


    //司机修改密码 http://127.0.0.1:9090/reset/verify?passwd=123456&id=1
    private static final String RESET_VERIFY = "/reset/verify";
    public static void postDriverResetVerify(RequestParams params, JsonHttpResponseHandler jsonHttpResponseHandler){
        WebUtil.post(RESET_VERIFY, params, jsonHttpResponseHandler);
    }
    public static RequestParams getRpDriverResetVerify(String id,String passwd){
        RequestParams params = new RequestParams();
        params.add("id", id);
        params.add("passwd", passwd);
        return  params;
    }

    //更改司机账号状态为上线 http://127.0.0.1:9090/login/alter?lastlogintime='2017-04-30 22:22:22'&id=1&status=1
    private static final String LOGIN_ALTER = "/login/alter";
    public static void postLoginAlter(RequestParams params, JsonHttpResponseHandler jsonHttpResponseHandler){
        WebUtil.post(LOGIN_ALTER, params, jsonHttpResponseHandler);
    }
    public static RequestParams getRpLoginAlter(String id,String status,String lastlogintime){
        RequestParams params = new RequestParams();
        params.add("id", id);
        params.add("status", status);
        params.add("lastlogintime", lastlogintime);
        return  params;
    }

    //更改司机账号状态为下线 http://127.0.0.1:9090/exit/alter?id=1&status=0
    private static final String EXIT_ALTER = "/exit/alter";
    public static void postExitAlter(RequestParams params, JsonHttpResponseHandler jsonHttpResponseHandler){
        WebUtil.post(EXIT_ALTER, params, jsonHttpResponseHandler);
    }
    public static RequestParams getRpExitAlter(String id,String status){
        RequestParams params = new RequestParams();
        params.add("id", id);
        params.add("status", status);
        return  params;
    }

    //修改司机车辆状态 http://127.0.0.1:9090/state/alter?id=1&state=2
    private static final String STATE_ALTER = "/state/alter";
    public static void postStateAlter(RequestParams params, JsonHttpResponseHandler jsonHttpResponseHandler){
        WebUtil.post(STATE_ALTER, params, jsonHttpResponseHandler);
    }
    public static RequestParams getRpStateAlter(String id,String state){
        RequestParams params = new RequestParams();
        params.add("id", id);
        params.add("state", state);
        return  params;
    }

    //列出竞价中的所有订单 http://127.0.0.1:9090/package/list
    private static final String PACKAGE_LIST = "/package/list";
    public static void getPackageList(JsonHttpResponseHandler jsonHttpResponseHandler){
        WebUtil.get(PACKAGE_LIST, null, jsonHttpResponseHandler);
    }

    //根据id查询订单的详细信息 http://127.0.0.1:9090/package/search?id=1
    private static final String PACKAGE_SEARCH = "/package/search";
    public static void postPackageSearch(RequestParams params, JsonHttpResponseHandler jsonHttpResponseHandler){
        WebUtil.post(PACKAGE_SEARCH, params, jsonHttpResponseHandler);
    }
    public static RequestParams getRpPackageSearch(String id){
        RequestParams params = new RequestParams();
        params.add("id", id);
        return  params;
    }

    //订单报价，添加竞价用户和报价 http://127.0.0.1:9090/bidding/add?uid=1&price=99&uname=王小&pid=9
    private static final String BIDDING_ADD = "/bidding/add";
    public static void postBiddingAdd(RequestParams params, JsonHttpResponseHandler jsonHttpResponseHandler){
        WebUtil.post(BIDDING_ADD, params, jsonHttpResponseHandler);
    }
    public static RequestParams getRpBiddingAdd(String uid,String pid,String price,String uname){
        RequestParams params = new RequestParams();
        params.add("uid", uid);
        params.add("price", price);
        params.add("uname", uname);
        params.add("pid", pid);
        return  params;
    }

    //取消报价 http://127.0.0.1:9090/bidding/remove?uid=1&pid=9
    private static final String BIDDING_REMOVE = "/bidding/remove";
    public static void postBiddingRemove(RequestParams params, JsonHttpResponseHandler jsonHttpResponseHandler){
        WebUtil.post(BIDDING_REMOVE, params, jsonHttpResponseHandler);
    }
    public static RequestParams getRpBiddingRemove(String uid,String pid){
        RequestParams params = new RequestParams();
        params.add("uid", uid);
        params.add("pid", pid);
        return  params;
    }


    //判断用户是否参与该订单的竞价 http://127.0.0.1:9090/bidding/search?uid=1&pid=1
    private static final String BIDDING_SEARCH = "/bidding/search";
    public static void postBiddingSearch(RequestParams params, JsonHttpResponseHandler jsonHttpResponseHandler){
        WebUtil.post(BIDDING_SEARCH, params, jsonHttpResponseHandler);
    }
    public static RequestParams getRpBiddingSearch(String uid,String pid){
        RequestParams params = new RequestParams();
        params.add("uid", uid);
        params.add("pid", pid);
        return  params;
    }



    //列出用户已完成的订单 http://127.0.0.1:9090/completed/list?uid=1
    private static final String COMPLETED_LIST = "/completed/list";
    public static void postCompletedList(RequestParams params, JsonHttpResponseHandler jsonHttpResponseHandler){
        WebUtil.post(COMPLETED_LIST, params, jsonHttpResponseHandler);
    }
    public static RequestParams getRpCompletedList(String uid){
        RequestParams params = new RequestParams();
        params.add("uid", uid);
        return  params;
    }


    //定时上报位置 http://127.0.0.1:9090/location/add?lon=222.222&lat=333.333&timestamp=2017&uid=1
    private static final String LOCATION_ADD = "/location/add";
    public static void postLocationAdd(RequestParams params, JsonHttpResponseHandler jsonHttpResponseHandler){
        WebUtil.post(LOCATION_ADD, params, jsonHttpResponseHandler);
    }
    public static RequestParams getRpLocation(String uid,String lon,String lat,String timestamp){
        RequestParams params = new RequestParams();
        params.add("uid", uid);
        params.add("lon", lon);
        params.add("lat", lat);
        params.add("timestamp", timestamp);
        return  params;
    }



}
